package jaux.tank.bean.constant.factory;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;

import jaux.tank.bean.constant.state.Order;
import jaux.tank.utils.HttpKit;
import jaux.tank.utils.ToolUtil;
import jaux.tank.utils.factory.Page;
/**
 * BootStrap Table默认的分页参数创建
 *
 * @date 2017-04-05 22:25
 */
public class PageFactory<T> {

	@Deprecated
    public Page<T> defaultPage() {
        HttpServletRequest request = HttpKit.getRequest();
        //每页多少条数据
        int limit = Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("limit"),"20"));
        String pageNum = request.getParameter("page");
        //每页的偏移量(本页当前有多少条)
        int offset = 0;
        if(StringUtils.isNotEmpty(pageNum)){
            offset = (Integer.parseInt(pageNum)-1)*limit;
        }else {

            offset = Integer.parseInt(org.apache.commons.lang3.StringUtils.defaultIfEmpty(request.getParameter("offset"),"0"));
        }
        //排序字段名称
        String sort = request.getParameter("sort");
        //asc或desc(升序或降序)
        String order = request.getParameter("order");
        if (ToolUtil.isEmpty(sort)) {
            Page<T> page = new Page<>((offset / limit + 1), limit);
            page.setOpenSort(false);
            return page;
        } else {
            Page<T> page = new Page<>((offset / limit + 1), limit, sort);
            page.setAsc(Order.ASC.getDes().equals(order));
            return page;
        }
    }
	public static <E> Page<E> getRequestPage() {
		HttpServletRequest request = HttpKit.getRequest();
		//每页多少条数据
		int limit = Integer.parseInt(StringUtils.defaultIfEmpty(request.getParameter("limit"),"20"));
		String pageNum = request.getParameter("page");
		//每页的偏移量(本页当前有多少条)
		int offset = 0;
		if(StringUtils.isNotEmpty(pageNum)){
			offset = (Integer.parseInt(pageNum)-1)*limit;
		}else {
			
			offset = Integer.parseInt(org.apache.commons.lang3.StringUtils.defaultIfEmpty(request.getParameter("offset"),"0"));
		}
		//排序字段名称
		String sort = request.getParameter("sort");
		//asc或desc(升序或降序)
		String order = request.getParameter("order");
		if (ToolUtil.isEmpty(sort)) {
			Page<E> page = new Page<>((offset / limit + 1), limit);
			page.setOpenSort(false);
			return page;
		} else {
			Page<E> page = new Page<>((offset / limit + 1), limit, sort);
			page.setAsc(Order.ASC.getDes().equals(order));
			return page;
		}
	}
}
